#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include"model.h"

typedef struct s_chunk *chunk;
struct s_chunk
{
	FILE *fp; 	// le fp est situe juste avant la data principale du chunk
		  	//avant le traitement, puis pointe sur le debut du chunk suivant apres
	unsigned long int length; // entier contenant la taille du chunk
	unsigned char *name; // tableau de 4 char contenant le nom
	unsigned char *crc;  // tableau de 4 char contenant le crc
};

typedef struct s_IHDR *IHDR;
struct s_IHDR
{
	unsigned long int w;
	unsigned long int h;
	unsigned char depth;
	unsigned char coltype;
	unsigned char compression;
	unsigned char filter;
	unsigned char interlace;

};

void readSig(FILE *fp, char *name)
{
	unsigned char *sig = malloc (sizeof(unsigned char)*8);
	fread(sig, 8, 1, fp);

	if (strcmp(sig, "\x89PNG\r\n\x1A\n"))
	{
		fseek(fp, 3, SEEK_CUR);
		printf("successing in opening \" %s \" (PNG signature recognized)\n", name);
	}
	else
	{
		printf(" \" %s \" is not a PNG file (PNG signature altered)\n", name);
		fclose(fp);
	}
}

chunk chunkDetect(FILE *fp)
{
	unsigned long int *length = malloc(sizeof(unsigned long int));
	unsigned char *name = malloc(sizeof(unsigned char)*4);
	unsigned char *crc = malloc(sizeof(unsigned char)*4);
	
	fread(length, 4, 1, fp);
	fread(name, 4, 1, fp);
	fseek(fp, *length, SEEK_CUR);
	fread(crc, 4, 1, fp);
	fseek(fp, -(*length), SEEK_CUR);

	chunk ch = malloc(sizeof(struct s_chunk));
	ch->fp = fp;
	ch->length = *length;
	ch->name = name;
	ch->crc = crc;

	return(ch);
}

FILE IHDRDetect (chunk ch)
{
	printf("getting the IHDR\n");
	unsigned long int *width = malloc (sizeof(unsigned long int)); // int sur 4 octets
	unsigned long int *height = malloc (sizeof(unsigned  long int)); // int sur 4 octets
	unsigned char *depth = malloc (sizeof(unsigned char)); // int sur 1 octet
	unsigned char *colType = malloc (sizeof(unsigned char)); // int sur 1 octet
	unsigned char *compression = malloc (sizeof(unsigned char)); // 1 octet
	unsigned char *filter = malloc (sizeof(unsigned char)); // 1 octet
	unsigned char *interlace = malloc (sizeof(unsigned char)); // 1 octet

	FILE *fp = ch->fp;
	fread(width, 4, 1, fp);
	fread(height, 4, 1, fp);
	fread(depth, 1, 1, fp);
	fread(colType, 1, 1, fp);
	fread(compression, 1, 1, fp);
	fread(filter,1, 1, fp);
	fread(interlace, 1, 1, fp);

	IHDR i = malloc(sizeof(struct s_IHDR));
	i->w = *width;
	i->h = *height;
	i->depth = *depth;
	i->coltype = *colType;
	i->compression = *compression;
	i->filter = *filter;
	i->interlace = *interlace;


	fseek(fp, 4, SEEK_CUR);
	ch->fp = fp;

	printf("IHDR Stocked\n");
	return *fp;
}

void IDATDetect (chunk ch)
{
	printf("lal");
}

void IENDDetect(chunk ch)
{
	printf("lol");
}

FILE skipChunk (chunk ch)
{
	FILE *fp;
	if (strcmp(ch->name, "IHDR"))
		*fp = IHDRDetect(ch);
	else if (strcmp(ch->name, "IDAT"))
		IDATDetect(ch);
	else if (strcmp(ch->name, "IEND"))
		IENDDetect(ch);
	else
	{
		FILE *fp = ch->fp;
		fseek(fp, ch->length, SEEK_CUR); // skip le data field
		fseek(fp, 4, SEEK_CUR); // skip le CRC
	}
	return *fp;
}

void openFile(char *name)
{
	char bufSize = 32;
	char buffer[bufSize];
	FILE *fp = fopen(name,"r");
	if (!fp)
		printf("Failed to open file \" %s \", this file doesn't exist", name);
	else
	{
		readSig(fp, name);
		while (!feof(fp))
		{
			*fp = skipChunk(chunkDetect(fp));
		}
	}
	fclose(fp);
}

int main(int ac, char *av[])
{
	if (ac == 2)
		openFile(av[1]);
	else
	{
		perror("Think, you fool!");
		return 1;
	}
	return 0;
} 
